package com.kws.system.service.contractTemplateService.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.domain.R;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.contractManagementDao.Rq.CreateContractTemplateRq;
import com.kws.system.domain.contractManagementDao.TbContractTemplate;
import com.kws.system.mapper.contractTemplateMapper.ContractTemplateMapper;
import com.kws.system.domain.contractManagementDao.info.QueryContractTemplateInfo;
import com.kws.system.service.contractTemplateService.ContractTemplateFileService;
import com.kws.system.service.contractTemplateService.ContractTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <b>ContractTemplateServiceImpl</b>
 *
 * @description: ContractTemplateServiceImpl <br>
 * @date: 2024/11/18 13:49 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ContractTemplateServiceImpl extends ServiceImpl<ContractTemplateMapper, TbContractTemplate> implements ContractTemplateService {
    private static final Logger logger = LoggerFactory.getLogger(ContractTemplateServiceImpl.class);

    @Autowired
    private ContractTemplateFileService contractTemplateFileService;

    @Override
    public R<Page<QueryContractTemplateInfo>> queryContractTemplatePage(Integer pageNum, Integer pageSize, String name, Integer type) {
        Page<QueryContractTemplateInfo> page = new Page<>(pageNum, pageSize);
        Page<QueryContractTemplateInfo> pageInfo = baseMapper.queryContractTemplatePage(page,name,type);
        return R.ok(pageInfo);
    }

    @Override
    public boolean createContractTemplate(CreateContractTemplateRq rq) {
        TbContractTemplate template = new TbContractTemplate();
        template.setName(rq.getName());
        template.setType(rq.getType());
        template.setTemplateType("0");
        Ksys.setBasicModelData(template,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(template);
        if (1 == insert){
            contractTemplateFileService.createcontractTemplateFile(template.getId(),rq.getFileUrls());
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteContractTemplateById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("create_user",SecurityUtils.getStringUserId());
        updateWrapper.set("create_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public List<String> queryTemplateFileById(Long id) {
        return contractTemplateFileService.queryTemplateFileById(id);
    }
}
